using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Data.SQLite;
using Common;

namespace Webgame.Access
{
    partial class Top
    {
        static object addLock = new object();

        string insertSQL = "insert into Top([MapID],[PlayerID],[Score],[Rank],[AddTime])  values(@MapID,@PlayerID,@Score,@Rank,@AddTime)";
        /// <summary>
        /// 添加一条新的记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Webgame.Entity.Top model)
        {
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("@MapID", model.MapID),
                new SQLiteParameter("@PlayerID", model.PlayerID),
                new SQLiteParameter("@Score", model.Score),
                new SQLiteParameter("@Rank", model.Rank),
                new SQLiteParameter("@AddTime", model.AddTime),};
            lock (addLock)
            {
                int row = SQLiteHelper.ExecuteNonQuery(insertSQL, parameters);

                DataSet ds = SQLiteHelper.ExecuteDataset("select max(id) from player");
                int id = (int)(Int64)ds.Tables[0].Rows[0][0];
                model.ID = id;
            }

            return model.ID;
        }

        string updateSQL = "update Top Set [MapID]=@MapID,[PlayerID]=@PlayerID,[Score]=@Score,[Rank]=@Rank,[AddTime]=@AddTime where [ID]=@ID";        /// <summary>
        /// 更新一个对象
        /// </summary>
        /// <param name="model"></param>
        public void Update(Webgame.Entity.Top model)
        {
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("@ID", model.ID),
                new SQLiteParameter("@MapID", model.MapID),
                new SQLiteParameter("@PlayerID", model.PlayerID),
                new SQLiteParameter("@Score", model.Score),
                new SQLiteParameter("@Rank", model.Rank),
                new SQLiteParameter("@AddTime", model.AddTime),};
            SQLiteHelper.ExecuteNonQuery(updateSQL, parameters);
        }

        string deleteSQL = "delete from Top where [ID]=@ID";

        /// <summary>
        /// 删除一个对象
        /// </summary>
        /// <param name="model"></param>
        public void Delete(Webgame.Entity.Top model)
        {
            Delete(model.ID);
        }

        /// <summary>
        /// 通过关键字段ID删除一条记录
        /// </summary>
        /// <param name="ID"></param>
        public void Delete(int ID)
        {
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("@ID", ID)};

            SQLiteHelper.ExecuteNonQuery(deleteSQL, parameters);

        }

        string selectSQL = "select * from Top";

        /// <summary>
        /// 根据关键字段ID获得一条数据记录
        /// 如果不存在记录则返回NULL
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public Webgame.Entity.Top GetTopByID(int ID)
        {
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("@ID", ID)};

            string sql = selectSQL + " where ID=@ID";

            IDataReader reader = SQLiteHelper.ExecuteReader(sql, parameters);

            return GetSingle(reader);
        }

        /// <summary>
        /// 从返回的记录里获得第一条数据
        /// 如果没有数据，则返回NULL
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        private static Webgame.Entity.Top GetSingle(IDataReader reader)
        {
            Webgame.Entity.Top ret = null;

            try
            {
                if (reader.Read())
                {
                    ret = GetModel(reader);
                }
            }
            finally
            {
                reader.Close();
            }

            return ret;
        }

        /// <summary>
        /// 获得数据库表里所有数据
        /// </summary>
        /// <returns></returns>
        public List<Webgame.Entity.Top> GetTopAll()
        {
            IDataReader reader = SQLiteHelper.ExecuteReader(selectSQL);

            return GetMultiEntity(reader);        
        }

        /// <summary>
        /// 从DataReader里获得多条记录
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        private static List<Webgame.Entity.Top> GetMultiEntity(IDataReader reader)
        {
            List<Webgame.Entity.Top> ret = new List<Webgame.Entity.Top>();

            try
            {
                while (reader.Read())
                {
                    ret.Add(GetModel(reader));
                }
            }
            finally
            {
                reader.Close();
            }

            return ret;
        }

        private static Webgame.Entity.Top GetModel(IDataReader reader)
        {
            Webgame.Entity.Top ret = new Webgame.Entity.Top();

            ret.ID = (int)(Int64)reader["ID"];
            ret.MapID = (int)(Int64)reader["MapID"];
            ret.PlayerID = (int)(Int64)reader["PlayerID"];
            ret.Score = (int)(Int64)reader["Score"];
            ret.Rank = (int)(Int64)reader["Rank"];
            ret.AddTime = (DateTime)reader["AddTime"];

            return ret;
        }
        static Top instance = null;

        public static Top Instance
        {
            get
            {
                if (instance == null)
                    instance = new Top();
                return Top.instance;
            }
        }

    }
}

